# -*- coding: utf-8 -*-
"""
SVM中使用多项式特征
Created on Wed Apr 25 20:37:51 2018

@author: Allen
"""

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
X, y = datasets.make_moons()

plt.scatter( X[y==0, 0], X[y==0, 1] )
plt.scatter( X[y==1, 0], X[y==1, 1] )
plt.show()

X, y = datasets.make_moons( noise = 0.15, random_state = 666 )
plt.scatter( X[y==0, 0], X[y==0, 1] )
plt.scatter( X[y==1, 0], X[y==1, 1] )
plt.show()

### 使用多项式特征的SVM
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline

def PolynomialSVC( degree, C = 1. ):
    return Pipeline([
                ( "poly", PolynomialFeatures() ),
                ( "std_scaler", StandardScaler() ),
                ( "linearsvc", LinearSVC( C = C ) )
            ])
poly_svc = PolynomialSVC( degree = 3 )
poly_svc.fit( X, y )

### 使用多项式核函数的svm
from sklearn.svm import SVC
def PolynomialKernelSVC( degree, C = 1. ):
    return Pipeline([
                ( "std_scaler", StandardScaler() ),
                ( "kernelSVC", SVC( kernel = "poly", degree = 3, C = C ) )
            ])
    
poly_kernel_svc = PolynomialSVC( degree = 3 )
poly_kernel_svc.fit( X, y )